package vip.xiaonuo.modular.grouporgrelatechangetemp.service;

import org.springframework.stereotype.Component;
import vip.xiaonuo.core.timer.TimerTaskRunner;
import vip.xiaonuo.facedoor.modular.device.entity.DeviceCategory;
import vip.xiaonuo.facedoor.modular.memberfeatures.entity.MemberFeatures;
import vip.xiaonuo.facedoor.modular.memberfeatures.param.MemberFeaturesParam;
import vip.xiaonuo.facedoor.modular.memberfeatures.service.MemberFeaturesService;
import vip.xiaonuo.megvii.config.model.ApiResponse;
import vip.xiaonuo.megvii.service.DeviceMegviiFaceGroupFaceTokenRelaService;
import vip.xiaonuo.modular.facegroupfacetokenrela.param.FaceGroupFaceTokenRelaSave;
import vip.xiaonuo.modular.grouporgrelatechangetemp.entity.GroupOrgRelateChangeTemp;
import vip.xiaonuo.modular.grouporgrelatechangetemp.param.GroupOrgRelateChangeTempParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class RelateChangeTaskRunner implements TimerTaskRunner {

    @Resource
    private GroupOrgRelateChangeTempService tempService;

    @Resource
    private MemberFeaturesService featureService;


    @Resource
    private DeviceMegviiFaceGroupFaceTokenRelaService groupTokenRelaService;

    @Override
    public void action(String jobParams,String taskId) {
        handle();
    }

    @Override
    public void executeXxlJob() throws InterruptedException {
        handle();
    }

    /**
     * 1. 查询出所有的未执行的数据
     * 2. 根据未执行数据，组装出绑定与解绑需要的数据结构
     * 任务处理
     */
    private void handle() {
        // 先查询所有未执行的数据
        GroupOrgRelateChangeTempParam param = new GroupOrgRelateChangeTempParam();
        param.setStatus(0);
        List<GroupOrgRelateChangeTemp> temps = tempService.list(param);

        for(GroupOrgRelateChangeTemp temp : temps ){
            // 修改成处理中
            temp.setStatus(1);
            tempService.saveOrUpdate(temp);
            int bizType = temp.getBizType();

            // 1.查出当前已有的入库信息
            MemberFeaturesParam featureParam = new MemberFeaturesParam();
            featureParam.setDwMemberUserId(temp.getUserId());
            featureParam.setPlatform(DeviceCategory.SUB_TYPE_BOX);
            List<MemberFeatures> features = featureService.listByUserId(featureParam);
            if(features == null || features.size()==0){
                temp.setStatus(2);
                tempService.saveOrUpdate(temp);
            }else{
                List<Long> changed = new ArrayList<>();
                FaceGroupFaceTokenRelaSave saveBean = new FaceGroupFaceTokenRelaSave();
                changed.add(temp.getFaceGroupId());
                if(bizType==0){ // 添加
                    saveBean.setAddRelate(changed);
                }else if(bizType==1){ // 删除
                    saveBean.setRemoveRelate(changed);
                }
                for(MemberFeatures feature : features){
                    saveBean.setFeaturesId(feature.getId());
                    ApiResponse resp  = groupTokenRelaService.saveFaceGroupRelation(saveBean);
                    if(resp.isSuccess()){
                        temp.setStatus(2);
                    }else{
                        temp.setStatus(3);
                        temp.setFailReason(resp.getMessage());
                    }
                    tempService.saveOrUpdate(temp);
                }
            }
        }
    }
}
